package com.maple.soa.thread;

import java.util.concurrent.TimeUnit;

/**
 * 创建线程
 * 1.使用Thread类创建实例
 * 2.使用Runnable接口
 *
 * @author lijian
 * @date 2021/4/24 22:16
 */
public class T01_Thread {

    public static void main(String[] args) {
        System.out.println("==主线程开始==");

        for (int i = 0; i < 5; i++) {
            new MyThread(String.valueOf(i)).start();
        }

        /**
         * 2.使用Runnable接口
         * 接口方式更加灵活、方便
         *
         * @author lijian
         * @date 2021/4/26 0:16
         */
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                // 模拟耗时
                try {
                    TimeUnit.MICROSECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Runnable接口子线程==>" + Thread.currentThread().getName() + "执行<==");
            }).start();
        }

        System.out.println("==主线程结束==");
    }
}

/**
 * 1.使用Thread类创建实例
 *
 * @author lijian
 * @date 2021/4/26 0:13
 */
class MyThread extends Thread {
    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        // 模拟耗时
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread类实现子线程==>" + this.getName() + "执行<==");
    }
}